#include "linksettings.h"
#include <QDir>
#include <QSettings>
#include <QApplication>

const char* key_download_default_dir_on = "download_default_dir_on";
const char* key_download_default_dir = "download_default_dir";
const char* key_debug = "debug";
const char* key_websocket_port_local = "local_websocket_port";
const char* key_websocket_port_remote = "remote_websocket_port";
const char* key_task_execute_name = "task_execute_name";

LinkSettings::LinkSettings():QObject(nullptr)
{
    init();
}

void LinkSettings::init(QString path)
{
    if (m_set) {
        m_set->deleteLater();
        m_set = nullptr;
    }

    if (path.isEmpty()) {
        auto dir = QDir(QApplication::applicationDirPath());
        path = dir.filePath("config.ini");
    }

    m_set = new QSettings(path, QSettings::Format::IniFormat, this);
}

QSettings *LinkSettings::config() const
{
    return m_set;
}

bool LinkSettings::getDownloadDefaultDirOn() const
{
    return m_set->value(key_download_default_dir_on, false).toBool();
}

void LinkSettings::setDownload_default_dir_on(bool download_default_dir_on)
{
    m_set->setValue(key_download_default_dir_on, download_default_dir_on);
}

QString LinkSettings::getDownloadDefaultDir() const
{
    return m_set->value(key_download_default_dir, "C:/").toString();
}

void LinkSettings::setDownload_default_dir(const QString &download_default_dir)
{
    m_set->setValue(key_download_default_dir, download_default_dir);
}

bool LinkSettings::getDebugEnabled() const
{
    return m_set->value(key_debug, false).toBool();
}

int LinkSettings::getLocalWebsocketPort() const
{
    auto port = m_set->value(key_websocket_port_local, 19034).toInt();
    Q_ASSERT(port > 1000 && port < 65535);

    return port;
}

int LinkSettings::getRemoteWebsocketPort() const
{
    auto port = m_set->value(key_websocket_port_remote, 8383).toInt();
    Q_ASSERT(port > 1000 && port < 65535);

    return port;
}

QString LinkSettings::getTaskExecuteName() const
{
    auto name = m_set->value(key_task_execute_name, "task.exe").toString();
    return name.isEmpty()?"task.bat":name;
}

